﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;

namespace Pearl
{
    public class SpRecognition
    {
        private static SpRecognition _Instance = null;
        private SpeechLib.ISpeechRecoGrammar isrg;
        private SpeechLib.SpSharedRecoContextClass ssrContex = null;
        private System.Windows.Forms.Control cDisplay;
        private SpRecognition()
        {
            ssrContex = new SpSharedRecoContextClass();
            isrg = ssrContex.CreateGrammar(1);
            SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
            new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);
            ssrContex.Recognition += recHandle;
        }
        public void BeginRec(Control tbResult)
        {
            isrg.DictationSetState(SpeechRuleState.SGDSActive);
            cDisplay = tbResult;
        }
        public static SpRecognition instance()
        {
            if (_Instance == null)
                _Instance = new SpRecognition();
            return _Instance;
        }
        public void CloseRec()
        {
            isrg.DictationSetState(SpeechRuleState.SGDSInactive);
        }
        private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result)
        {
            cDisplay.Text = result.PhraseInfo.GetText(0, -1, true);
        }

    } 
}
